package com.example.mypra1.fragments;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.clj.memoryspinner.MemorySpinner;
import com.example.mypra1.R;

import org.json.JSONObject;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 今日运势
 */
public class MoreFragment1 extends Fragment {
    private View rootView;
    private TextView summary1,color1,health1,xz1,love1,money1,number1,work1,total1;
    private String sat;//星座
    private MemorySpinner memorySpinner;
    private Handler handler=new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {//操作UI,将请求的运势数据渲染
            Map<String,Object>map = (Map<String, Object>) msg.obj;
            summary1.setText(map.get("summary").toString());
            color1.setText(map.get("color").toString());
            health1.setText(map.get("health").toString());
            xz1.setText(map.get("xz").toString());
            love1.setText(map.get("love").toString());
            money1.setText(map.get("money").toString());
            number1.setText(map.get("number").toString());
            work1.setText(map.get("work").toString());
            total1.setText(map.get("total").toString());
        }
    };

    public static MoreFragment1 newInstance() {
        Bundle args = new Bundle();
        MoreFragment1 fragment = new MoreFragment1();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_more1, container, false);
        }
        SharedPreferences pref = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
        sat = pref.getString("constellation","金牛座");
        color1 = rootView.findViewById(R.id.color1);
        health1 = rootView.findViewById(R.id.health1);
        money1 = rootView.findViewById(R.id.money1);
        work1 = rootView.findViewById(R.id.work1);
        number1 = rootView.findViewById(R.id.number1);
        xz1 = rootView.findViewById(R.id.xz1);
        total1 = rootView.findViewById(R.id.total1);
        love1 = rootView.findViewById(R.id.love1);
        summary1 = rootView.findViewById(R.id.summary1);
        memorySpinner = rootView.findViewById(R.id.ms1);
        ArrayList<String> list = new ArrayList<>(Arrays.asList("白羊座","金牛座","双子座","巨蟹座","狮子座","处女座",
                "天枰座","天蝎座","射手座","摩羯座","水瓶座","双鱼座"));
        memorySpinner.setData(null,list);
        memorySpinner.setSelection(list.indexOf(sat));
        memorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                sat = list.get(i);
                getYS();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        getYS();//请求数据
        return rootView;
    }
    //发送get请求，获取今日运势
    private void getYS(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String date = dateFormat.format(new Date());
                    OkHttpClient okHttpClient = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://web.juhe.cn/constellation/getAll?consName="+sat+"&type=today&key=70a442015077d1f7a27eec861c7228bc")
                            .build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException,RuntimeException {
                            Log.d("resp",response.toString());
                            if (response.isSuccessful()) {
                                String res = response.body().string();
                                try {
                                    JSONObject jsonObject = new JSONObject(res);
                                    Map<String,String> map = new HashMap<>();
                                    map.put("color",jsonObject.getString("color"));
                                    map.put("number",jsonObject.getString("number"));
                                    map.put("health",jsonObject.getString("health"));
                                    map.put("money",jsonObject.getString("money"));
                                    map.put("love",jsonObject.getString("love"));
                                    map.put("xz",jsonObject.getString("QFriend"));
                                    map.put("work",jsonObject.getString("work"));
                                    map.put("total",jsonObject.getString("all"));
                                    map.put("summary",jsonObject.getString("summary"));
                                    Message message = new Message();
                                    message.obj = map;
                                    handler.sendMessage(message);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }else{
                                Log.d("resp",response.toString());
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}